package com.qqbot.impl.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.JiuLiApiService;
import com.qqbot.impl.utils.HttpClientUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 久黎API服务
 *
 * @author changwenpeng
 * @date 2022年5月11日
 */
@Service
public class JiuLiApiServiceImpl implements JiuLiApiService {

    @Override
    public MessageChain xiaoAi(String keyword) throws Exception {
        String url = "http://jiuli.xiaoapi.cn/i/chat/xiaoai_tts.php?msg=" + keyword;
        String resJson = HttpClientUtils.get(url, 10000, 10000);
        if (StringUtils.hasText(resJson)) {
            JSONObject json = JSONObject.parseObject(resJson);
            String mp3 = json.getString("mp3");
            if (StringUtils.hasText(mp3)) {
                mp3 = mp3.replaceAll("\u003d", "=");
                return new MessageChain().record(mp3);
            }
        }
        return new MessageChain();
    }

}
